package com.xbongbong.workflow.domain.entity.ext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.domain.entity.ext.CommitVerifyConditionEntityExt;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.FieldPermissionEntityExt;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.enums.WorkflowNodeTypeEnum;
import com.xbongbong.workflow.pojo.nodes.ApprovalNode;
import com.xbongbong.workflow.pojo.nodes.CcNode;
import com.xbongbong.workflow.pojo.nodes.FormTriggerNode;
import com.xbongbong.workflow.pojo.nodes.SendMsgNode;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author chenpeng
 * @version 1.0
 * @date 2021-05-20 10:14
 * @description
 */
@Setter
@Getter
public class WorkflowNodeEntityExt extends WorkflowNodeEntity {
    
    private static final long serialVersionUID = 581389901573861831L;
    
    /**
     * 负责人list TODO 手动拼装
     */
    private List<OptionalRangeEntity> mainUserList;
    
    /**
     * 抄送人list TODO 手动拼装
     */
    private List<OptionalRangeEntity> ccUserList;
    
    /**
     * 转交人list TODO 手动拼装
     */
    private List<OptionalRangeEntity> transferUserList;
    
    /**
     * 打回节点list
     */
    private List<Long> backNodeList;
    
    /**
     * 字段权限
     */
    private List<FieldPermissionEntityExt> fieldPermissionList;
    
    private Map<String, FieldPermissionEntityExt> fieldPermissionMap;
    /**
     * 节点校验条件列表
     */
    private List<CommitVerifyConditionEntityExt> commitVerifyConditionList;
    /**
     * 解析后的流转条件
     */
    private List<ConditionsEntityExt> conditions;
    
    /**
     * 表单数据id
     */
    private Long formDataId;

    private Long workflowNodeId;
    /**
     * 老审批节点，用于映射
     */
    private Long processNodeId;

    /**
     * 新的节点的uid(条件分支复制那边使用)
     */
    private String newUid;

    /**
     * 新的节点wrapperId(条件分支复制那边使用)
     */
    private String newWrapperId;
    
    public void setMainUserList(String mainUser) {
        this.mainUserList = JSONArray.parseArray(mainUser, OptionalRangeEntity.class);
    }
    
    public void setCcUserList(String ccUser) {
        this.ccUserList = JSONArray.parseArray(ccUser, OptionalRangeEntity.class);
    }
    
    public void setTransferUserList(String transferUser) {
        this.transferUserList = JSONArray.parseArray(transferUser, OptionalRangeEntity.class);
    }
    
    public void setBackNodeList(String backNodeList) {
        this.backNodeList = JSONArray.parseArray(backNodeList, Long.class);
    }
    
    public void setFieldPermissionList(String fieldPermission) {
        this.fieldPermissionList = JSONArray.parseArray(fieldPermission, FieldPermissionEntityExt.class);
    }
    
    public void setCommitVerifyConditionList(String commitVerifyCondition) {
        this.commitVerifyConditionList = JSONArray.parseArray(commitVerifyCondition, CommitVerifyConditionEntityExt.class);
    }
    
    public void setFormDataId(Long formDataId) {
        this.formDataId = formDataId;
    }
    
    public void setConditions(String condition) {
        this.conditions = JSONArray.parseArray(condition, ConditionsEntityExt.class);
    }
    
    public void setConditionByClass(List<ConditionsEntityExt> conditionList){
        this.conditions = conditionList;
    }
    
    public void setFieldPermissionMap(Map<String, FieldPermissionEntityExt> fieldPermissionMap) {
        this.fieldPermissionMap = fieldPermissionMap;
    }

    public List<FieldPermissionEntityExt> getFieldPermissionList() {
        fieldPermissionList = new ArrayList<>();
        if (Objects.nonNull(this.getConfigData())) {
            WorkflowNodeTypeEnum workflowNodeTypeEnum = WorkflowNodeTypeEnum.getByType(this.getType());
            switch (workflowNodeTypeEnum) {
                case START_NODE:
                    FormTriggerNode formTriggerNode = JSON.parseObject(this.getConfigData(), FormTriggerNode.class);
                    fieldPermissionList = new ArrayList<>(formTriggerNode.getFieldPermission().values());
                    break;
                case END_NODE:
                    break;
                case APPROVE_NODE:
                    ApprovalNode approvalNode = JSON.parseObject(this.getConfigData(), ApprovalNode.class);
                    fieldPermissionList = new ArrayList<>(approvalNode.getFieldPermission().values());
                    break;
                case CC_NODE:
                    CcNode ccNode = JSON.parseObject(this.getConfigData(), CcNode.class);
                    fieldPermissionList = new ArrayList<>(ccNode.getFieldPermission().values());
                    break;
                case CONDITION_NODE:
                case CONDITION_BLOCK_NODE:
                case CREATE_NODE:
                case UPDATE_NODE:
                case DELETE_NODE:
                case MULTI_DATA_NODE:
                case SINGLE_DATA_NODE:
                    break;
                case SEND_MESSAGE_NODE:
                    SendMsgNode sendMsgNode = JSON.parseObject(this.getConfigData(), SendMsgNode.class);
                    fieldPermissionList = new ArrayList<>(sendMsgNode.getFieldPermission().values());
                    break;
                default:

            }
        }
        return fieldPermissionList;
    }

    public List<OptionalRangeEntity> getTransferUserList() throws XbbException, InstantiationException, IllegalAccessException {
        transferUserList = new ArrayList<>();
        if (Objects.nonNull(this.getConfigData())) {
            WorkflowNodeTypeEnum workflowNodeTypeEnum = WorkflowNodeTypeEnum.getByType(this.getType());
            switch (workflowNodeTypeEnum) {
                case START_NODE:
                case END_NODE:
                    break;
                case APPROVE_NODE:
                    ApprovalNode approvalNode = JSON.parseObject(this.getConfigData(), ApprovalNode.class);
                    List<OptionalRangeEntity> transferUser = approvalNode.getTransferUserList() == null ? new ArrayList<>() : approvalNode.getTransferUserList();
                    BeanUtil.copyPropertiesList(transferUser, transferUserList, OptionalRangeEntity.class);
                    break;
                case CC_NODE:
                case CONDITION_NODE:
                case CONDITION_BLOCK_NODE:
                case CREATE_NODE:
                case UPDATE_NODE:
                case DELETE_NODE:
                case MULTI_DATA_NODE:
                case SINGLE_DATA_NODE:
                case SEND_MESSAGE_NODE:
                default:

            }
        }
        return transferUserList;
    }


    public  WorkflowNodeEntityExt() {
    }


    public WorkflowNodeEntityExt(String corpid, String name, Integer triggerType, Integer type, Long appId, Long formId, Long menuId, Integer saasMark, Integer businessType, Long workflowId, String uid, String prevId, String wrapperId, Integer priority, String configData) {
        super.setCorpid(corpid);
        super.setName(name);
        super.setTriggerType(triggerType);
        this.setType(type);
        this.setAppId(appId);
        this.setFormId(formId);
        this.setMenuId(menuId);
        this.setSaasMark(saasMark);
        this.setBusinessType(businessType);
        this.setWorkflowId(workflowId);
        this.setUid(uid);
        this.setPrevId(prevId);
        this.setWrapperId(wrapperId);
        this.setPriority(priority);
        this.setConfigData(configData);
        this.setAddTime(DateTimeUtil.getInt());
        this.setUpdateTime(DateTimeUtil.getInt());
        this.setDel(DelEnum.NORMAL.getDel());
    }

    public WorkflowNodeEntityExt(String corpid, String name, Integer triggerType, Integer type, Long appId, Long formId, Long menuId, Integer saasMark, Integer businessType, Long workflowId, String uid, String prevId, String wrapperId, Integer priority, String configData, Long processNodeId) {
        super.setCorpid(corpid);
        super.setName(name);
        super.setTriggerType(triggerType);
        this.setType(type);
        this.setAppId(appId);
        this.setFormId(formId);
        this.setMenuId(menuId);
        this.setSaasMark(saasMark);
        this.setBusinessType(businessType);
        this.setWorkflowId(workflowId);
        this.setUid(uid);
        this.setPrevId(prevId);
        this.setWrapperId(wrapperId);
        this.setPriority(priority);
        this.setConfigData(configData);
        this.setAddTime(DateTimeUtil.getInt());
        this.setUpdateTime(DateTimeUtil.getInt());
        this.setDel(DelEnum.NORMAL.getDel());
        this.processNodeId = processNodeId;
    }

}
